package com.sys.ai.controller;

import com.sys.ai.domain.AiKnowledgeFragmentation;
import com.sys.ai.service.IAiKnowledgeFragmentationService;
import com.sys.common.annotation.Log;
import com.sys.common.core.controller.BaseController;
import com.sys.common.core.domain.AjaxResult;
import com.sys.common.core.page.TableDataInfo;
import com.sys.common.enums.BusinessType;
import com.sys.common.utils.poi.ExcelUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 知识库分片Controller
 *
 * @author LGY
 * @date 2025-05-26
 */
@Tag(name = "知识库分片")
@RestController
@RequestMapping("/ai/knowledgeFragmentation")
public class AiKnowledgeFragmentationController extends BaseController {
    @Autowired
    private IAiKnowledgeFragmentationService aiKnowledgeFragmentationService;

    /**
     * 查询知识库分片列表
     */
    @PostMapping("/list")
    @Operation(summary = "查询知识库分片分页列表")
    public TableDataInfo list(@RequestBody AiKnowledgeFragmentation aiKnowledgeFragmentation) {
        startPage(aiKnowledgeFragmentation);
        List<AiKnowledgeFragmentation> list = aiKnowledgeFragmentationService.selectAiKnowledgeFragmentationList(aiKnowledgeFragmentation);
        return getDataTable(list);
    }

    @GetMapping("/listAllByKnowledgeId/{knowledgeId}")
    @Operation(summary = "根据知识库id查询切片列表")
    public AjaxResult listAllByKnowledgeId(@PathVariable("knowledgeId") String knowledgeId) {
        List<AiKnowledgeFragmentation> list = aiKnowledgeFragmentationService.listAllByKnowledgeId(knowledgeId);
        return success(list);
    }

    @GetMapping("/listAllByKnowledgeFileId/{knowledgeFileId}")
    @Operation(summary = "根据知识库文件id查询切片列表")
    public AjaxResult listAllByKnowledgeFileId(@PathVariable("knowledgeFileId") String knowledgeFileId) {
        List<AiKnowledgeFragmentation> list = aiKnowledgeFragmentationService.listAllByKnowledgeFileId(knowledgeFileId);
        return success(list);
    }


    /**
     * 导出知识库分片列表
     */
    @Log(title = "知识库分片", businessType = BusinessType.EXPORT)
    @Operation(summary = "导出知识库分片")
    @PostMapping("/export")
    public void export(HttpServletResponse response, AiKnowledgeFragmentation aiKnowledgeFragmentation) {
        List<AiKnowledgeFragmentation> list = aiKnowledgeFragmentationService.selectAiKnowledgeFragmentationList(aiKnowledgeFragmentation);
        ExcelUtil<AiKnowledgeFragmentation> util = new ExcelUtil<AiKnowledgeFragmentation>(AiKnowledgeFragmentation.class);
        util.exportExcel(response, list, "知识库分片数据");
    }

    /**
     * 获取知识库分片详细信息
     */
    @Operation(summary = "查询知识库分片详情")
    @GetMapping(value = "/detail/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(aiKnowledgeFragmentationService.selectAiKnowledgeFragmentationById(id));
    }

    /**
     * 新增知识库分片
     */
    @Log(title = "知识库分片", businessType = BusinessType.INSERT)
    @Operation(summary = "新增知识库分片")
    @PostMapping("/save")
    public AjaxResult add(@RequestBody AiKnowledgeFragmentation aiKnowledgeFragmentation) {
        aiKnowledgeFragmentationService.insertAiKnowledgeFragmentation(aiKnowledgeFragmentation);
        return success();
    }

    /**
     * 修改知识库分片
     */
    @Log(title = "知识库分片", businessType = BusinessType.UPDATE)
    @Operation(summary = "修改知识库分片")
    @PutMapping("/update")
    public AjaxResult edit(@RequestBody AiKnowledgeFragmentation aiKnowledgeFragmentation) {
        aiKnowledgeFragmentationService.updateAiKnowledgeFragmentation(aiKnowledgeFragmentation);
        return success();
    }

    /**
     * 删除知识库分片
     */
    @Operation(summary = "删除知识库分片")
    @Log(title = "知识库分片", businessType = BusinessType.DELETE)
    @DeleteMapping("/remove/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        aiKnowledgeFragmentationService.deleteAiKnowledgeFragmentationByIds(ids);
        return success();
    }

    @GetMapping("/queryFragmentationBySearch/{knowledgeId}/{search}")
    @Operation(summary = "处理查询分片")
    public AjaxResult queryFragmentationBySearch(@PathVariable("knowledgeId") String knowledgeId, @PathVariable("search") String search) {
        String data = aiKnowledgeFragmentationService.queryFragmentationBySearch(knowledgeId, search);
        return success().put("data", data);
    }
}